/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */
//原创微博视图

import UIKit
import SnapKit
import SDWebImage

class LSStatusOriginView: UIView {
    
    //记录原创微博底部结果
    var originViewConstraint: Constraint?
    
    var statusViewModel: LSStatusViewModel? {
        didSet {
            if let profile = statusViewModel?.status?.user?.profile_image_url {
                //绑定头像数据
                headImageView.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //绑定昵称
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            //设定微博数据
            contentLabel.attributedText = statusViewModel?.origianlAttributedString
            //来源
            sourceLabel.text = statusViewModel?.sourceContent
            //设置会员等级图片
            mbrankImageView.image = statusViewModel?.mbrankImage
            //设置认证类型等级图片
            verifiedTypeImageView.image = statusViewModel?.verifiedTypeImage
            //设置时间
            timeLabel.text = statusViewModel?.timeContent
            
            //卸载上次的约束
            originViewConstraint?.uninstall()
            
            //判断原创微博的配图个数是否大于0
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0 {
                //有图大于0,显示配图
                pictureView.hidden = false
                //绑定原创微博数据源
                pictureView.picUrls = picUrls
                
                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin)
                })
                
            } else {
                //隐藏配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.originViewConstraint = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
        }
    }
    
    //MARK: - 懒加载
    //头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel: UILabel = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    //会员等级
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.orangeColor())
    //来源
    private lazy var sourceLabel: UILabel = UILabel(fontSize: 12, textColor: UIColor.lightGrayColor())
    //微博内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel(fontSize: StatusFontSize, textColor: UIColor.darkGrayColor())
        //多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //配图
    private lazy var pictureView: LSStatusPictureView = {
        let view = LSStatusPictureView()
        view.backgroundColor = self.backgroundColor
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 添加控件设置约束
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(headImageView)
        addSubview(verifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(mbrankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //timeLabel.text = "大苗"
        
        //设置约束,@noescape表示闭包不能被其他属性持有,在闭包里面可以省略self
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(headImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        //关键约束,原创微博视图的底部视图 = 微博内容的地步约束 + 间距
        self.snp_makeConstraints { (make) -> Void in
            self.originViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
    }
}













